package bootstrap

import (
	"game-server/global"

	"github.com/minio/minio-go"
	"go.uber.org/zap"
)

func InitMinIO() error {
	minioInfo := global.App.Config.Minio

	minioClient, err := minio.New(
		minioInfo.Endpoint,
		minioInfo.AccessKeyID,
		minioInfo.SecretAccessKey,
		minioInfo.UseSSL,
	)
	if err != nil {
		return err
	}

	//客户端注册到全局变量中
	global.App.MinioClient = minioClient

	// 确保 bucket 存在
	exists, err := minioClient.BucketExists(
		minioInfo.BucketName,
	)
	global.App.Log.Info("minio bucket exists", zap.Bool("exists", exists), zap.String("bucket", minioInfo.BucketName))

	if err != nil {
		return err
	}
	if !exists {
		err = minioClient.MakeBucket(minioInfo.BucketName, minioInfo.Location)
		if err != nil {
			return err
		}
	}
	//创建一个叫userheader的存储桶。
	// utils.CreateMinoBuket("userheader")
	return nil
}
